﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class EntityAreaComponent : MonoBehaviour
{
	private NavMeshModifier modifier;
	private Transform tran;
	private CEntity entity;
	public float updateTime = 0.1f;
	private float time = 0;
	public static EntityAreaComponent Create(Transform parent)
	{
		GameObject go = new GameObject("area");
		go.transform.SetParent(parent, false);
		return go.AddComponent<EntityAreaComponent>();
	}

	public void Bind(CEntity entity,int area)
	{
		this.entity = entity;
		tran = entity.transform;
		var box = gameObject.AddComponent<BoxCollider>();
		box.size = new Vector3(entity.bodyRadius * 2, 0.0001f, entity.bodyRadius * 2);
		gameObject.AddComponent<NavMeshSourceTag>();
		modifier = gameObject.AddComponent<NavMeshModifier>();
		modifier.overrideArea = true;
		modifier.area = area;
		Update();
	}
	void Update()
    {
		time -= Time.deltaTime;
		if (time <= 0)
		{
			Vector3 pos = tran.position;
			pos.y = -0.01f;
			transform.position = pos;
			time = updateTime;
		}
	}
}
